package common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.util.StringUtils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


/**
 * @author Seattle
 * 
 */
@SuppressWarnings("restriction")
public class ImageUtils {
	
	public static final String  MER_LOG_SIZE = "160*160";//商户logo图片
	
	private static final int MER_LOG_TYPE = 1;
	
	public static final String  MER_DETIAL_SIZE = "640*430";//商户介绍图片
	
	private static final int MER_DETIAL_TYPE = 2;
	
	public static final String  GD_LOG_SIZE = "160*120";//商品logo图片
	
	private static final int GD_LOG_TYPE = 3;
	
	public static final String  GD_DETIAL_SIZE = "460*280";//商品介绍图片
	
	private static final int GD_DETIAL_TYPE = 4;
	
	public static final String  ADV_DETIAL_SIZE = "400*140";//广告图片
	
	private static final int ADV_DETIAL_TYPE = 5;
	
	public static final String  UNIACT_MIN_SIZE = "160*120";//银联活动小图片
	
	private static final int UNIACT_TYPE = 6;
	
	public static final String  UNIACT_NAX_SIZE = "460*280";//银联活动小图片

	/**
	 * 对字节数组字符串进行Base64解码并生成图片
	 * 
	 * @param imgBase64Str
	 *            base64编码字符串
	 * @param imgFilePath
	 *            图片生成的绝对路径
	 * @return
	 */
	public static boolean GenerateImage(String imgBase64Str,
			String imgFilePath, String imageName) {
		System.out.println("开始生成图片。。。。。。。");
		if (StringUtils.isEmpty(imgBase64Str)) // 图像数据为空
			return false;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			// Base64解码
			byte[] b = decoder.decodeBuffer(imgBase64Str);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}

			String createPath = imgFilePath;
			File imgeFile = new File(createPath);
			if (!imgeFile.exists()) {
				mkDir(imgeFile);
			}

			OutputStream out = new FileOutputStream(imgFilePath + "/"
					+ imageName);
			out.write(b);
			out.flush();
			out.close();
			System.out.println("生成图片成功。。。。。。。");
			return true;
		} catch (Exception e) {
			System.out.println("生成图片失败。。。。。。。");
			e.printStackTrace();
			return false;
		}
	}

	public static String GetImageStr(String imgFilePath) {// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
		byte[] data = null;

		// 读取图片字节数组
		try {
			InputStream in = new FileInputStream(imgFilePath);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 对字节数组Base64编码
		BASE64Encoder encoder = new BASE64Encoder();
		return encoder.encode(data);// 返回Base64编码过的字节数组字符串
	}

	/**
	 * 获取当前日期字符串   为文件夹
	 * @return
	 */
	public static String getDateStr() {
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		return simpleDateFormat.format(date);
	}

	/**
	 * 循环创建文件夹
	 * @param file
	 */
	public static void mkDir(File file) {
		if (file.getParentFile().exists()) {
			file.mkdir();
		} else {
			mkDir(file.getParentFile());
			file.mkdir();
		}
	}
}
